
#include "stdafx.h"
#include "ui_dialogfactory.h"

#include "ui_applicationwindow.h"



/**
 * Provides a new window for editing the active block.
 */
void UI_DialogFactory::requestEditBlockWindow(PF_BlockList* blockList) {
    PF_DEBUG->print("QC_DialogFactory::requestEditBlockWindow()");

    UI_ApplicationWindow* appWindow = UI_ApplicationWindow::getAppWindow();
    UI_MDIWindow* parent = appWindow->getMDIWindow();
    if (parent!=NULL) {
        //PF_BlockList* blist = blockWidget->getBlockList();
        if (blockList!=NULL) {
            PF_Block* blk = blockList->getActive();
            if (blk!=NULL) {
                UI_MDIWindow* w = appWindow->slotFileNew(blk);
                // the parent needs a pointer to the block window and
                //   vice versa
                parent->addChildWindow(w);
                w->getGraphicView()->zoomAuto(false);
            }
        }
    }
}



/**
 * Closes all windows that are editing the given block.
 */
void UI_DialogFactory::closeEditBlockWindow(PF_Block* block) {
	PF_DEBUG->print("QC_DialogFactory::closeEditBlockWindow");
	
	UI_ApplicationWindow* appWindow = UI_ApplicationWindow::getAppWindow();
	QWorkspace* workspace = appWindow->getWorkspace();

    if (workspace!=NULL) {
		PF_DEBUG->print("QC_DialogFactory::closeEditBlockWindow: workspace found");
		
        QWidgetList windows = workspace->windowList();
        for (int i = 0; i < int(windows.count()); ++i) {
			PF_DEBUG->print("QC_DialogFactory::closeEditBlockWindow: window: %d",
				i);
            UI_MDIWindow* m = (UI_MDIWindow*)windows.at(i);
            if (m!=NULL) {
				PF_DEBUG->print(
					"QC_DialogFactory::closeEditBlockWindow: got mdi");
				if (m->getDocument()==block) {
					PF_DEBUG->print(
						"QC_DialogFactory::closeEditBlockWindow: closing mdi");
					//m->closeMDI(true, false);
					m->close(true);
				}
			}
		}
	}
	appWindow->slotWindowActivated(NULL);
	
	PF_DEBUG->print("QC_DialogFactory::closeEditBlockWindow: OK");
}

